package com.com.xinke.edu.petmall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.com.xinke.edu.petmall.MainActivity;
import com.com.xinke.edu.petmall.databinding.ActivityLoginBinding;
import com.com.xinke.edu.petmall.entity.ResultBody;
import com.com.xinke.edu.petmall.entity.User;
import com.com.xinke.edu.petmall.event.RegisterEvent;
import com.com.xinke.edu.petmall.util.RetrofitUtil;
import com.com.xinke.edu.petmall.util.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityLoginBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());
        EventBus.getDefault().register(this);
        binding.etPhone.setText(SPUtils.getInstance(this).getString("account",""));
        binding.etPassword.setText(SPUtils.getInstance(this).getString("password",""));

        binding.tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent(LoginActivity.this, RegisterActivityActivity.class);
                startActivity(intent);
            }
        });
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });

    }

    public void login(){
        String account = binding.etPhone.getText().toString();
        String password = binding.etPassword.getText().toString();
        if(TextUtils.isEmpty(account)){
            Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(password)){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        RetrofitUtil.getApiService().login(account,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResultBody<User> userResultBody) {
                        User user = userResultBody.getData();
                        SPUtils.getInstance(LoginActivity.this).putLong("userId",user.getId());
                        if(binding.cbRemember.isChecked()){
                            SPUtils.getInstance(LoginActivity.this).putString("account",user.getUsername());
                            SPUtils.getInstance(LoginActivity.this).putString("password",user.getPassword());
                        }else{
                            SPUtils.getInstance(LoginActivity.this).putString("account","");
                            SPUtils.getInstance(LoginActivity.this).putString("password","");
                        }
                        if(user.getRole()==1){ //跳转管理员主界面

                        }else if(user.getRole()==2){  //跳转商家主页面
                            startActivity(new Intent(LoginActivity.this, PubProductActivity.class));
                        }else  if(user.getRole()==3){ //跳转用户主页面
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }



    @Subscribe(threadMode = ThreadMode.POSTING)
    public void registerEvent(RegisterEvent registerEvent){
        String account= registerEvent.getAcount();
        binding.etPhone.setText(account);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}